home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13115 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Message-ID: <31517CF6.7C6F@novell.com>
  2. Date: Thu, 21 Mar 1996 08:59:50 -0700
  3. From: Sukanta Ganguly <sukanta_ganguly@novell.com>
  4. Organization: Novell Inc
  5. X-Mailer: Mozilla 2.0 (WinNT; I)
  6. MIME-Version: 1.0
  7. Newsgroups: comp.lang.c++
  8. Subject: Re: object creation from an abstract base class
  9. References: <4hj285$c87@comet.magicnet.net>
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. NNTP-Posting-Host: sganguly.npd.provo.novell.com
  13. Path: news.provo.novell.com!
  14.  
  15. Michael Catello wrote:
  16. > Hello OOPsters,
  17. > I was just looking for validation/other suggestions for a method I
  18. > recently used in a program. I have defined an abstract base class
  19. > (i.e. contains pure virtual functions), all access to the derived
  20. > classes of this base are thru a pointer to the base class. To create
  21. > the actual objects of the derived classes I used the following scheme:
  22. > enum FooType {BAR, BAS};
  23. > // base class
  24. > class CFoo
  25. >         {
  26. >         CFoo();
  27. >         ~CFoo();
  28. >         static CFoo* CreateFoo(FooType type);
  29. >         // other methods/data including pure virtual fns whose behaviour will
  30. > be defined in the derived classes
  31. >         };
  32. > class CBar: public CFoo
  33. >         {
  34. >         //
  35. >         };
  36. > class CBas: public CFoo
  37. >         {
  38. >         //
  39. >         };
  40. > CFoo* CFoo::CreateFoo(FooType type)
  41. >         {
  42. >         CFoo* pfoo = NULL;
  43. >         switch (type)
  44. >                 {
  45. >                 case BAR:
  46. >                         pfoo = new CBar;
  47. >                         break;
  48. >                 case BAS:
  49. >                         pfoo = new CBas;
  50. >                         break;
  51. >                 }
  52. >         return pfoo;
  53. >         }
  54. > main()
  55. >         {
  56. >         CFoo* interface = CFoo::CreateFoo(BAR);
  57. >         }
  58. > Obviously it is the CreateFoo() function that I am wondering about. In
  59. > the actual implementation I had multiple static "Create" functions for
  60. > the base class that would allow me to create a new object: one based
  61. > on an enumerated token (shown above), another an existing object, as
  62. > well as one based on the format of a datafile. My application never
  63. > references any of the derived classes directly, except in their
  64. > creation and definition.
  65. > Is there another/better/more appropriate way to handle this type of
  66. > object creation? Thanks for your assistance,
  67. > Regards,
  68. > -Michael.
  69. > /*
  70. >  * catello@magicnet.net
  71. >  * http://www.magicnet.net/~catello
  72. >  * CompuServe: 70401,3661
  73. >  */Hi,
  74. I looked at your code snippet and was slightly confused. In your 
  75. CreateFoo method of CFoo class, the pfoo variable is a pointer 
  76. to CFoo which has no idea of what are CBas or CBar. But in your 
  77. code you are creating CBas as well as CBar objects and storing 
  78. it in pfoo. In your derived CBar and CBas you would obviously 
  79. have more data members which CFoo* would not be addressing. The 
  80. compiler should crib at you at  places like "pfoo = new CBas" 
  81. and "pfoo = new CBar". I wouldn't do it like this.
  82.  
  83. Thanx
  84.